package com.gba.mark.exam.controller;

import com.alibaba.fastjson.JSON;
import com.gba.mark.exam.entity.ExamPaper;
import com.gba.mark.exam.entity.ExamQuestions;
import com.gba.mark.exam.service.ExamPaperService;
import com.gba.mark.group.service.IGroupService;
import com.gba.mark.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping ("/exam/paper")
public class ExamPaperController {

    @Autowired
    private ExamPaperService examPaperService;

    @Resource
    private IGroupService groupService;

    /**
     * 新建并保存一份试卷
     */
    @PostMapping("/savePaper")
    public ResponseUtil<Integer> savePaper(Authentication authentication,
                                           @RequestBody String str,
                                           @RequestParam String groupName) {

        List<ExamQuestions> questionsList = JSON.parseArray(JSON.parseObject(str).getString("questionsList"),ExamQuestions.class);

        int group_id = groupService.getGroupIdByAuthAndName(authentication, groupName);


        return new ResponseUtil<>(HttpStatus.OK, "", examPaperService.savePaper(questionsList, group_id));

    }

    /**
     * 删除一份试卷
     */

    @DeleteMapping("/deleteExamPaper")
    public ResponseUtil<Boolean> deleteExamPaper(Authentication authentication,
                                                 @RequestParam String groupName) {

        int group_id = groupService.getGroupIdByAuthAndName(authentication,groupName);
        return new  ResponseUtil<>(HttpStatus.OK,"",examPaperService.removeExamPaper(group_id));
    }


    /**
     * 给新入组的用户展示试卷(查询用户的试卷)
     */
    @GetMapping("/listExamPaper")
    public ResponseUtil<List<ExamPaper>> listExamPaper(Authentication authentication,
                                                       @RequestParam String groupName){
        int group_id = groupService.getGroupIdByAuthAndName(authentication, groupName);
        return new ResponseUtil<>(HttpStatus.OK, "", examPaperService.findPaper(group_id));
    }


 }
